package com.fish.gaiety.gateway.service.impl;

import com.fish.gaiety.gateway.base.BaseAudit;
import com.fish.gaiety.gateway.entity.dto.Authorities;
import com.fish.gaiety.gateway.entity.dto.User;
import com.fish.gaiety.gateway.repository.AuthoritiesRepository;
import com.fish.gaiety.gateway.service.AuthoritiesService;
import com.fish.gaiety.gateway.service.UserServiceDeprecated;
import com.fish.gaiety.gateway.util.BaseAuthorities;
import lombok.RequiredArgsConstructor;

//@Component
/**
 * 把方法都移动到{@link UserServiceDeprecatedImpl}里面，
 * 解决User和权限的循环依赖
 * 本来User和权限应该都是一致的
 */
@Deprecated
@RequiredArgsConstructor
public class AuthoritiesServiceImpl implements AuthoritiesService {

    private final AuthoritiesRepository authoritiesRepository;

    private final UserServiceDeprecated userServiceDeprecated;

    @Override
    public void grantBaseAuthority(User user, BaseAuthorities baseAuthorities) {
        Authorities authorities = new Authorities();
        authorities.setBaseAuthorities(baseAuthorities);
        authorities.setUser(user);
        authoritiesRepository.save(authorities);
    }

    @Override
    public Authorities getBaseAuthorityByUserInfo(String userId) {
        User adminUser = userServiceDeprecated.getUser(userId);
        return authoritiesRepository.findByUser(adminUser);
    }

    @Override
    @BaseAudit
    public Authorities getBaseAuthorityByUserInfoEnhance(String userId) {
        User adminUser = userServiceDeprecated.getUser(userId);
        return authoritiesRepository.findByUser(adminUser);
    }
}
